package com.weetop.aklyscan.appConfig;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.weetop.aklyscan.bean.User;

import java.util.ArrayList;
import java.util.HashMap;

public class MyApplication extends Application {
    private static Context mContext;
    private ArrayList<String> testList = new ArrayList<>();
    //获取上下文
    private static String userToken;
    private static String port;
    private static String userName;
    private static String mailCode;
    private static String mailID;
    public static Boolean b_scaleIsConnect = Boolean.FALSE;
    public static int isBluePlay = 0;
    public static int isDyPlay = 0;
    public static Boolean dy_scaleIsConnect = Boolean.FALSE;

    private static SharedPreferences sp;
    private static SharedPreferences.Editor edit;
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public static String getUserToken() {
        return userToken == null ? "" : userToken;
    }

    /**
     * 2---生产商
     *
     * @return
     */
    public static String getPort() {
        return port == null ? "" : port;
    }

    public static String getUserName() {
        return userName == null ? "" : userName;
    }

    public static String getMailCode() {
        return mailCode == null ? "" : mailCode;
    }

    public static String getMailID() {
        return mailID == null ? "" : mailID;
    }

    public static void setUserToken(String userToken) {
        MyApplication.userToken = userToken;
        if (edit != null) {
            edit.putString("userToken", userToken);
            edit.commit();
        }
    }

    public static void setPort(String port) {
        MyApplication.port = port;
        if (edit != null) {
            edit.putString("port", port);
            edit.commit();
        }
    }

    public static void setUserName(String userName) {
        MyApplication.userName = userName;
        if (edit != null) {
            edit.putString("userName", userName);
            edit.commit();
        }
    }

    public static void setMailCode(String mailCode) {
        MyApplication.mailCode = mailCode;
        if (edit != null) {
            edit.putString("mailCode", mailCode);
            edit.commit();
        }
    }

    public static void setMailID(String mailID) {
        MyApplication.mailID = mailID;
        if (edit != null) {
            edit.putString("mailID", mailID);
            edit.commit();
        }
    }

    public HashMap<String, String> getGlobalData() {
        HashMap<String, String> map = new HashMap<>();

        return map;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        mContext = this;
        //  QMUISwipeBackActivityManager.init(this);
        sp = getSharedPreferences("persondata", Context.MODE_PRIVATE);
        edit = sp.edit();
        userToken = sp.getString("userToken", "");
        port = sp.getString("port", "");
        userName = sp.getString("userName", "");
        mailCode = sp.getString("mailCode", "");
        mailID = sp.getString("mailID", "");

        initLogger();
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        testList.add("ss");
        CrashReport.initCrashReport(getApplicationContext(), "07eeff882f", false);
    }

    public ArrayList<String> getTestList() {
        return testList;
    }

    public void setTestList(ArrayList<String> testList) {
        this.testList = testList;
    }

    public static Context getContext() {
        return mContext;
    }

    private void initLogger() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // 是否显示线程信息 默认显示 上图Thread Infrom的位置
                .methodCount(0)         // 展示方法的行数 默认是2  上图Method的行数
                .methodOffset(7)        // 内部方法调用向上偏移的行数 默认是0
//                .logStrategy(customLog) // 改变log打印的策略一种是写本地，一种是logcat显示 默认是后者（当然也可以自己定义）
                .tag("Weetop:")   // 自定义全局tag 默认：PRETTY_LOGGER
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, String tag) {
                return true;
            }
        });

    }

    static {
        //全局设置默认的 Header
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
            layout.setEnableHeaderTranslationContent(true);
            return new ClassicsHeader(context).setDrawableArrowSize(14).setDrawableSize(14).setEnableLastTime(false);
        });
        //全局设置默认的 footer
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {

            //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
            layout.setEnableHeaderTranslationContent(true);
            return new ClassicsFooter(context).setDrawableArrowSize(14).setDrawableSize(14);
        });
    }
}
